<?php
/**
 * Created by PhpStorm.
 * User: zhd
 * Date: 2019/3/19
 * Time: 21:40
 */

namespace app\Module\Index\Controller;


use app\Module\Index\Logic\MessageLogic;
use app\Module\Index\Logic\MessageType;
use app\Module\Index\Model\FriendsModel;

class FriendsController extends CheckAuth
{
    /**
     * 修改好友关系
     * @verify int $user_id
     * @return string
     */
    public function setRelationAction($user_id)
    {
        if ($user_id == session('userId')) {
            return fail('自己不能关注自己');
        }
        $small_user_id = min($user_id, session('userId'));
        $big_user_id   = max($user_id, session('userId'));

        $friendsModel = new FriendsModel();
        $friendsModel->addWhere([
            'small_user_id' => $small_user_id,
            'big_user_id'   => $big_user_id,
        ]);
        $data = $friendsModel->row(null, 600);

        if (empty($data)) {
            if (session('userId') == $small_user_id) {
                $relation = 0;
            } else {
                $relation = 1;
            }
            // 我关注
            $friendsModel->insert([
                'small_user_id' => $small_user_id,
                'big_user_id'   => $big_user_id,
                'created'       => now(),
                'updated'       => now(),
                'relation'      => $relation,
            ]);

            $messageLogic = new MessageLogic();
            $messageLogic->add($this->userId, $user_id, $this->userId, MessageType::USER_ATTENTION, '');
        } else {
            // 删除好友关系
            if (($data['relation'] == 0 && $small_user_id == session('userId')) ||
                $data['relation'] == 1 && $big_user_id == session('userId')) {
                $relation = -1;
                $friendsModel->delete(
                    [
                        'small_user_id' => $small_user_id,
                        'big_user_id'   => $big_user_id,
                    ]
                );
            } elseif (($data['relation'] == 1 && $small_user_id == session('userId')) ||
                $data['relation'] == 0 && $big_user_id == session('userId')) {
                // 相互关注
                $relation = 2;
                $friendsModel->update(
                    [
                        'relation' => $relation,
                        'updated'  => now(),
                    ],
                    [
                        'small_user_id' => $small_user_id,
                        'big_user_id'   => $big_user_id,
                    ]
                );

                $messageLogic = new MessageLogic();
                $messageLogic->add($this->userId, $user_id, $this->userId, MessageType::USER_ATTENTION, '');
            } elseif ($data['relation'] == 2) {
                if (session('userId') == $small_user_id) {
                    $relation = 1;
                } else {
                    $relation = 0;
                }
                // 我单项取消关注
                $friendsModel->update(
                    [
                        'relation' => $relation,
                        'updated'  => now(),
                    ],
                    [
                        'small_user_id' => $small_user_id,
                        'big_user_id'   => $big_user_id,
                    ]
                );
            }
        }

        return success($relation);
    }

    /**
     * 获取好友列表
     * @verify int|null $page
     * @verify int|null $limit
     * @return string
     */
    public function getFriendsAction($page = 1, $limit = 20)
    {
        $user_id      = session('userId');
        $friendsModel = new FriendsModel();
        $friendsModel->addWhere([
            'relation' => 2,
        ]);
        $friendsModel->setFields("user.*");
        $friendsModel->addWhere("and (small_user_id='{$user_id}' or big_user_id='{$user_id}')");
        $friendsModel->setJoin('user', "(
            small_user_id = '{$user_id}'
            AND `user`.id = big_user_id
        )
        OR (
            small_user_id != '{$user_id}'
            AND `user`.id = small_user_id
        )");
        $friendsModel->setPage($page, $limit);
        $friendsModel->addOrder('user.username');
        $data = $friendsModel->select();
        return success($data);
    }

    /**
     * 获取粉丝列表
     * @verify int|null $page
     * @verify int|null $limit
     */
    public function getFollowerAction($page = 1, $limit = 20)
    {
        $user_id      = session('userId');
        $friendsModel = new FriendsModel();
        $friendsModel->setFields("user.*");
        $friendsModel->addWhere("(small_user_id='{$user_id}' and relation=1) or (big_user_id='{$user_id}' and relation=0)");
        $friendsModel->setJoin('user', "(
            small_user_id = '{$user_id}'
            AND `user`.id = big_user_id
        )
        OR (
            small_user_id != '{$user_id}'
            AND `user`.id = small_user_id
        )");
        $friendsModel->setPage($page, $limit);
        $friendsModel->addOrder('user.username');
        $data = $friendsModel->select();
        return success($data);
    }

    /**
     * 获取关注列表
     * @verify int|null $page
     * @verify int|null $limit
     */
    public function getAttentionAction($page = 1, $limit = 20)
    {
        $user_id      = session('userId');
        $friendsModel = new FriendsModel();
        $friendsModel->setFields("user.*");
        $friendsModel->addWhere("(small_user_id='{$user_id}' and relation=0) or (big_user_id='{$user_id}' and relation=1)");
        $friendsModel->setJoin('user', "(
            small_user_id = '{$user_id}'
            AND `user`.id = big_user_id
        )
        OR (
            small_user_id != '{$user_id}'
            AND `user`.id = small_user_id
        )");
        $friendsModel->setPage($page, $limit);
        $friendsModel->addOrder('user.username');
        $data = $friendsModel->select();
        return success($data);
    }
}